using System.Configuration;
using System.Data;
using Microsoft.Win32;

namespace resharperupdate.core.services.impl
{
    public class RegistryLookup : IRegistryLookup
    {
        static readonly string KEY = ConfigurationManager.AppSettings["reg"];
        public string version()
        {
            RegistryKey serviceNames = Registry.LocalMachine.OpenSubKey(KEY) ?? Registry.CurrentUser.OpenSubKey(KEY);
            if (serviceNames == null) 
            {
                throw new DataException("Couldn't find resharper");
            }
            if(serviceNames.GetValue("ProductVersion")!=null)
            {
                return (string) serviceNames.GetValue("ProductVersion");
            }
            if(serviceNames.GetValue("One-Time Initialization Identity")!=null)
            {
                return ((string) serviceNames.GetValue("One-Time Initialization Identity")).Replace("Version=","");
            }
            throw new DataException("Couldn't find resharper");
        }
    }
}